package com.qumitech.visualnovel.controller.rest;

import com.qumitech.visualnovel.component.util.CommonUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.model.dao.console.EggPoundDO;
import com.qumitech.visualnovel.pubdef.dto.poundegg.EggPoundQueryDTO;
import com.qumitech.visualnovel.pubdef.dto.poundegg.UserPoundStatisticAdminQueryDTO;
import com.qumitech.visualnovel.service.poundegg.PoundEggService;
import com.google.common.collect.ImmutableMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/api/pound_egg")
public class PoundEggController {

    @Autowired
    private PoundEggService poundEggService;

    @RequestMapping(value = "/record/detail/export", method = RequestMethod.POST)
    public BaseRsp exportPoundEggDetail(@RequestBody EggPoundQueryDTO exDto, HttpServletResponse response) {
        poundEggService.exportPoundEgg(exDto, response);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @RequestMapping(value = "/record/detail/list", method = RequestMethod.GET)
    public BaseRsp listDetail(EggPoundQueryDTO queryDTO) {
        String[] stringTime = CommonUtil.setStartTime(queryDTO.getStartTime(), queryDTO.getEndTime());
        queryDTO.setStartTime(stringTime[0]);
        queryDTO.setEndTime(stringTime[1]);

        String[] timeArray = queryDTO.getStartTime().split("-");
        String tableName = "egg_pound_" + timeArray[0] + "_" + timeArray[1] + "_" + timeArray[2].split(" ")[0];
        queryDTO.setTableName(tableName);
        Integer count = poundEggService.countEggPound(queryDTO);
        List<EggPoundDO> eggPoundDOList = poundEggService.listEggPound(queryDTO);
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("count", count, "egg_pound", eggPoundDOList));
    }

    @GetMapping("/statistic/list")
    public BaseRsp poundEggStatisticList(UserPoundStatisticAdminQueryDTO queryDTO) {
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("user_pound_statistic", poundEggService.listPoundEggStatistic(queryDTO),
                "count", poundEggService.countPoundEggStatistic(queryDTO)));
    }
}


